/**
 * 
 */
package mediasManagement;

import hibernateUtils.HibernateUtil;

import java.util.List;

import dvdsManagement.Dvd;
import exceptions.*;
import mediasManagement.Media;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Restrictions;

import specimensManagement.Specimen;

/** 
 * @author Fabre, Malric
 */
public class MediaDAO {

	public static final String ISBN_ISAN = "isbnOrIsan";
	public static final String TITLE = "title";

	/**
	 * Adds a media in the persistence unit (hence in DB)
	 * 
	 * @param media
	 * @return boolean
	 */
	public static boolean add(Media media){
		Session session = HibernateUtil.openSession();
		try{
			for(Specimen spec : media.getSpecimens()){
				session.save(spec);
			}
			session.save(media);
		} catch (Exception e) {
			System.err.println("Problem when saving");
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public static boolean addSpecimen(Media media){
		Media m;
		Session session = HibernateUtil.openSession();
		try{
			m = get(media.getId());
		} catch (Exception e) {
			System.err.println("Problem when saving");
			e.printStackTrace();
			return false;
		}
		Specimen spec = new Specimen(m);
		try {
			Session session2 = HibernateUtil.openSession();
			session2.save(spec);
			m.addSpecimen(spec);
			session2.merge(m);
		} catch (Exception e) {
			System.err.println("Problem when saving");
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public static boolean removeSpecimen(Media media){
		Media m;
		Session session = HibernateUtil.openSession();
		try{
			m = get(media.getId());
		} catch (Exception e) {
			System.err.println("Problem when saving");
			e.printStackTrace();
			return false;
		}
		Specimen spec = new Specimen(m);
		try {
			Session session2 = HibernateUtil.openSession();
			session2.delete(spec);
			m.removeSpecimen(spec);
			session2.merge(m);
		} catch (Exception e) {
			System.err.println("Problem when saving");
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	/**
	 * Removes an instance of a media.
	 * 
	 * @param media
	 * @return boolean
	 * @throws DvdExistsException
	 */
	public static boolean remove(Media media){
		Session session = HibernateUtil.openSession();
		try{
			for(Specimen spec : media.getSpecimens()){
				session.delete(spec);
			}
			session.delete(media);
		} catch (Exception e) {
			System.err.println("Problem when saving");
			e.printStackTrace();
			return false;
		}
		return true;
	}

	/**
	 * Tests the presence of any media in DB
	 * 
	 * @return boolean
	 */
	@SuppressWarnings("unchecked")
	public boolean isEmpty(){
		Session session = HibernateUtil.openSession();
		Query query = session.createQuery("from medias");
		List<Media> medias = query.list();
		if (medias.isEmpty())
			return true;
		else
			return false;	
	}

	/**
	 * Gives the content of table medias in DB
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<Media> getContent() {
		Session session = HibernateUtil.openSession();
		Query query = session.createQuery("from medias");
		List<Media> media = query.list();

		session.close();
		return media;
	}
	
	public static Media get(String string) throws MediaExistsException {
		Session session = HibernateUtil.openSession();
		Media m = (Media) session.get(Media.class, string);
		if (m == null) throw new MediaExistsException();
		else return m;
	}

	public static boolean contains(Media media) throws MediaExistsException {
		Session session = HibernateUtil.openSession();
		if (media == null) throw new MediaExistsException();
		Media m = (Media) session.get(Media.class, media.getId());
		if (m == null) return false;
		else return m.equals(media);	
	}

	/**
	 * Gets an instance of a media by ID classification
	 * 
	 * @param mediaId
	 * @return media
	 * @throws DvdExistsException
	 */
	public static List<Media> getById(long mediaId) {
		Session session = HibernateUtil.openSession();
		Transaction t = session.beginTransaction();
		Criteria criteria = session.createCriteria(Media.class).add(Restrictions.eq("id", mediaId));
		List<Media> media = (List<Media>)criteria.list();
		t.commit();
		session.close();

		return media;
	}

	/**
	 * Gets an instance of a media by IsbnOrIsan classification
	 * 
	 * @param Object isbnOrIsan
	 * @return List
	 * @throws DvdExistsException
	 */
	public static List<Media> getByIsbnOrIsan(Object isbnOrIsan) throws DvdExistsException {
		Session session = HibernateUtil.openSession();
		Transaction t = session.beginTransaction();
		Criteria criteria = session.createCriteria(Media.class).add(Restrictions.eq(ISBN_ISAN, isbnOrIsan));
		List<Media> media = (List<Media>)criteria.list();
		t.commit();
		session.close();

		return media;
	}

	/**
	 * Gets an instance of a media by property classification
	 * 
	 * @param Object title
	 * @return List
	 * @throws DvdExistsException
	 */
	public static List<Media> getByTitle(Object title) throws DvdExistsException {
		Session session = HibernateUtil.openSession();
		Transaction t = session.beginTransaction();
		Criteria criteria = session.createCriteria(Media.class).add(Restrictions.eq(TITLE, title));
		List<Media> media = (List<Media>)criteria.list();
		t.commit();
		session.close();

		return media;
	}

	/**
	 * Number of medias in DB.
	 * 
	 * @return 
	 * @throws DvdExistsException
	 */
	public long size(){
		Session session = HibernateUtil.openSession();
		Query query = session.createQuery("from medias");
		List<Media> medias = query.list();
		return medias.size();
	}

	/**
	 * 
	 * @param media
	 * @return
	 */
	//	public static boolean addSpecimen(Media media){
	//		Media m;
	//		EntityManager entityManager=JPAUtil.getEntityManager();
	//		EntityTransaction tx=entityManager.getTransaction();
	//		tx.begin();
	//
	//		try{
	//			m= get(media.getId());
	//			tx.commit();
	//			JPAUtil.closeEntityManager();
	//
	//		} catch (Exception e) {
	//			System.err.println("Problem when saving");
	//			e.printStackTrace();
	//			return false;
	//		}
	//		Specimen specimen = new Specimen(m);
	//		try {
	//			entityManager=JPAUtil.getEntityManager();
	//			tx=entityManager.getTransaction();
	//			tx.begin();
	//			entityManager.persist(specimen);
	//			m.addSpecimen(specimen);
	//			entityManager.merge(m);
	//		} catch (Exception e) {
	//			System.err.println("Problem when saving");
	//			e.printStackTrace();
	//			return false;
	//		}
	//		tx.commit();
	//		JPAUtil.closeEntityManager();
	//		return true;
	//	}
	//
	//	/**
	//	 * 
	//	 * @param i
	//	 * @return
	//	 * @throws MediaExistsException
	//	 */
	//	public static Media get(int i) throws MediaExistsException {
	//		EntityManager entityManager=JPAUtil.getEntityManager();
	//		Media m = (Media) entityManager.find(Media.class, i);
	//		if (m ==null) throw new MediaExistsException();
	//		else return m;
	//	}
	//
	//	/**
	//	 * 
	//	 * @param specimen
	//	 * @return
	//	 */
	//	public boolean addSpecimen(Specimen specimen){
	//		EntityManager entityManager=JPAUtil.getEntityManager();
	//		try{
	//			entityManager.persist(specimen);
	//		} catch (Exception e) {
	//			System.err.println("Problem when saving");
	//			e.printStackTrace();
	//			return false;
	//		}
	//		return true;
	//	}
	//
	//	/**
	//	 * 
	//	 * @param media
	//	 * @return
	//	 * @throws MediaExistsException
	//	 */
	//	public static boolean contains(Media media) throws MediaExistsException {
	//		EntityManager entityManager=JPAUtil.getEntityManager();
	//		if (media == null) throw new MediaExistsException();
	//		Media m = (Media) entityManager.find(Media.class, media.getId());
	//		if (m ==null) return false;
	//		else return m.equals(media);	
	//	}
}


